package com.xinsoft.controller.mes;

import cn.hutool.core.util.ObjectUtil;
import com.xinsoft.common.Response;
import com.xinsoft.entity.dto.MesCraftDTO;
import com.xinsoft.entity.dto.MesDispatchPageParam;
import com.xinsoft.entity.dto.MesProcessReportPageParam;
import com.xinsoft.entity.po.MesDispatch;
import com.xinsoft.entity.po.MesProcessReport;
import com.xinsoft.service.MesDispatchHisService;
import com.xinsoft.service.MesDispatchService;
import com.xinsoft.service.MesProcessReportService;
import com.xinsoft.validator.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 派工记录
 */
@RestController
@RequestMapping("/scm/processReport")
public class MesProcessReportController {

    @Autowired
    private MesProcessReportService mesProcessReportService;
    @Autowired
    private MesDispatchService mesDispatchService;


    /**
     * 删除
     */
    @PostMapping("/del")
    public Response<?> del(@RequestBody List<Long> ids) {
        //todo 结案状态的单据不能删除
        mesProcessReportService.removeByIds(ids);
        return Response.succeed();
    }

    /**
     * 分页
     */
    @PostMapping("/pageList")
    public Response<?> pageList(@RequestBody MesProcessReportPageParam mesDispatchPageParam) {
        return mesProcessReportService.findPageListByParam(mesDispatchPageParam);
    }

    /**
     * 查看图片
     */
    @GetMapping("/getDocument")
    public Response<?> getDocument(@RequestParam Integer id) {
        return mesProcessReportService.getDocument(id);
    }

    /**
     * 该功能不需要了，所以没有完善
     * @param mesProcessReport
     * @return
     */
    @PostMapping("/edit")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> edit(@RequestBody MesProcessReport mesProcessReport) {
        if (ObjectUtil.isEmpty(mesProcessReport.getId())){
            return Response.fail("操作失败！");
        }
        MesProcessReport reportDO = mesProcessReportService.getById(mesProcessReport.getId());
        MesDispatch dispatchDO = mesDispatchService.getById(reportDO.getDispatchId());
        if ((ObjectUtil.isEmpty(dispatchDO.getCompleteNum()) ? BigDecimal.ZERO : dispatchDO.getCompleteNum()).compareTo(
                (ObjectUtil.isEmpty(dispatchDO.getPlanNum()) ? BigDecimal.ZERO : dispatchDO.getPlanNum())) > 0
        )   return Response.fail("该派工单已结案！");
        //todo 校验工序计划是否已经结案

        //1、更新派工单
        BigDecimal subtract = (ObjectUtil.isEmpty(mesProcessReport.getCompletionCount()) ? BigDecimal.ZERO : mesProcessReport.getCompletionCount()).
                subtract(ObjectUtil.isEmpty(reportDO.getCompletionCount()) ? BigDecimal.ZERO : reportDO.getCompletionCount());
        BigDecimal completeNum = (ObjectUtil.isEmpty(dispatchDO.getCompleteNum()) ? BigDecimal.ZERO : dispatchDO.getCompleteNum()).
                add(subtract);
        dispatchDO.setCompleteNum(completeNum);
        mesDispatchService.updateById(dispatchDO);
        //todo  2、更新工序计划

        //3、更新本表记录
        mesProcessReportService.updateById(mesProcessReport);
        return Response.succeed();
    }
}
